package com.jilian.powerstation.manege.Weather;

import android.app.Activity;

import com.jilian.powerstation.modul.activity.WeatherActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class WeaterManager {

    public void getWeather(String httpurl, double lat, double lon, HttpCallBack httpCallBack) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                BufferedReader bufferedReader = null;
                try {
                    URL url = new URL(String.format(httpurl+"?lat=%s&lon=%s&units=metric&appid=2bc3b26a66ee66bc4c861c09534bc782",lat,lon));// 根据自己的服务器地址填写
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(10000);
                    conn.setDoOutput(true);// 允许输出
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("Charset", "GBK");
                    OutputStream os = conn.getOutputStream();
                    os.write("name=allen".getBytes());
                    if (conn.getResponseCode() == 200) {
                        System.out.println(conn.toString());
                        InputStream is = conn.getInputStream();
                        InputStreamReader isr = new InputStreamReader(is, "GBK");
                        bufferedReader = new BufferedReader(isr);
                    }
                    String result = "";
                    String line = "";
                    if (bufferedReader != null) {
                        try {
                            while ((line = bufferedReader.readLine()) != null) {
                                result += line;
                            }

                        } catch (IOException e) {
                            if (httpCallBack != null) {
                                httpCallBack.onError(e);
                            }
                        }
                    }
                    if (httpCallBack != null) {
                        httpCallBack.onSuccess(result);

                    }
                } catch (MalformedURLException e) {
                    // URL格式错误
                    if (httpCallBack != null) {
                        httpCallBack.onError(e);
                    }
                } catch (UnsupportedEncodingException e) {
                    // 不支持你设置的编码
                    if (httpCallBack != null) {
                        httpCallBack.onError(e);
                    }
                } catch (ProtocolException e) {
                    // 请求方式不支持
                    if (httpCallBack != null) {
                        httpCallBack.onError(e);
                    }
                } catch (IOException e) {
                    // 输入输出通讯出错
                    if (httpCallBack != null) {
                        httpCallBack.onError(e);
                    }
                }
            }
        });
        thread.start();
    }

    public interface HttpCallBack<T> {
        void onSuccess(T t);

        void onError(Exception e);
    }
}
